package com.yizooo.tallybook;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.yizooo.tallybook.adapter.ChartVPAdapter;
import com.yizooo.tallybook.db.DBManager;
import com.yizooo.tallybook.frag_chart.IncomeChartFragment;
import com.yizooo.tallybook.frag_chart.OutcomeChartFragment;
import com.yizooo.tallybook.utils.CalendarDialog;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class MonthChartActivity extends AppCompatActivity {
    Button inBtn,outBtn;
    TextView dateTv,inTv,outTv;
    ViewPager chartVp;
    private int year;
    private int month;
    int selectPos=-1,selectMonth=-1;
    List<Fragment>chartFragList;
    private IncomeChartFragment incomeChartFragment;
    private OutcomeChartFragment outcomeChartFragment;
    private ChartVPAdapter chartVPAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tally_month_chart);
        initView();
        initTime();
        initStatistics(year,month);
        initFrag();
        setVPSelectListener();
    }

    private void setVPSelectListener() {
        chartVp.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageSelected(int position) {
                setButtonStyle(position);
            }
        });
    }
    private void initFrag() {

        chartFragList=new ArrayList<>();
        //创建fragment对象
        incomeChartFragment = new IncomeChartFragment();
        outcomeChartFragment = new OutcomeChartFragment();
        //添加数据到fragment中
        Bundle bundle = new Bundle();
        bundle.putInt("year",year);
        bundle.putInt("month",month);
        incomeChartFragment.setArguments(bundle);
        outcomeChartFragment.setArguments(bundle);
        //将fragment添加到数据源中
        chartFragList.add(outcomeChartFragment);
        chartFragList.add(incomeChartFragment);
        //设置适配器
        chartVPAdapter = new ChartVPAdapter(getSupportFragmentManager(), chartFragList);
        chartVp.setAdapter(chartVPAdapter);
        //将fragment加载到activity中

    }

    /*初始化某年某月地收支情况*/
    private void initStatistics(int year, int month) {
        float inMoneyOneMonth = DBManager.getSumMoneyOneMonth(year, month, 1);
        float outMoneyOneMonth = DBManager.getSumMoneyOneMonth(year, month, 0);
        int incountItemOneMonth = DBManager.getCountItemOneMonth(year, month, 1);
        int outcountItemOneMonth = DBManager.getCountItemOneMonth(year, month, 0);
        dateTv.setText(year+"年"+month+"月账单");
        inTv.setText("共"+incountItemOneMonth+"笔收入, ￥ "+inMoneyOneMonth);
        outTv.setText("共"+outcountItemOneMonth+"笔支出, ￥ "+outMoneyOneMonth);
    }

    /*初始化数据*/
    private void initTime() {
        Calendar calendar=Calendar.getInstance();
        year=calendar.get(Calendar.YEAR);
        month=calendar.get(Calendar.MONTH)+1;
    }

    //初始化控件
    private void initView() {
    inBtn=findViewById(R.id.chart_btn_in);
    outBtn=findViewById(R.id.chart_btn_out);
    dateTv=findViewById(R.id.chart_tv_date);
    inTv=findViewById(R.id.chart_tv_in);
    outTv=findViewById(R.id.chart_tv_out);
    chartVp=findViewById(R.id.chart_vp);
    }

    public void onClick(View view){
        int viewId=view.getId();
        if (viewId==R.id.chart_iv_back){
            finish();
        }else   if (viewId==R.id.chart_iv_rili){
            showriliDialog();
        }else  if (viewId==R.id.chart_btn_in){
            setButtonStyle(1);
            chartVp.setCurrentItem(1);
        }else  if (viewId==R.id.chart_btn_out){
            setButtonStyle(0);
            chartVp.setCurrentItem(0);
        }

    }
//显示日立的对话框
    private void showriliDialog() {
        CalendarDialog dialog = new CalendarDialog(this, selectPos, selectMonth);
        dialog.show();
        dialog.setDialogSize();
        dialog.setOnRefreshListenner(new CalendarDialog.OnRefreshListenner() {
            @Override
            public void onRefresh(int selPos, int year, int month) {
                MonthChartActivity.this.selectPos=selPos;
                MonthChartActivity.this.selectMonth=month;
                initStatistics(year,month);
                incomeChartFragment.setDate(year,month);
                outcomeChartFragment.setDate(year,month);
            }
        });
    }

    /**
     * 设置按钮样式改变  支出0收入1
     */
    private void setButtonStyle(int kind){
        if(kind==0){
            outBtn.setBackgroundResource(R.drawable.main_recordbtn_bg);
            outBtn.setTextColor(Color.WHITE);
            inBtn.setBackgroundResource(R.drawable.dialog_btn_bg);
            inBtn.setTextColor(Color.BLACK);
        }else if(kind==1){
            inBtn.setBackgroundResource(R.drawable.main_recordbtn_bg);
            inBtn.setTextColor(Color.WHITE);
            outBtn.setBackgroundResource(R.drawable.dialog_btn_bg);
            outBtn.setTextColor(Color.BLACK);
        }
    }
}
